﻿<%@ page import="com.qipay.baas.model.Menu" %>
<%@ page import="com.qipay.baas.model.MenuExample" %>
<%@ page import="com.qipay.baas.model.User" %>
<%@ page import="com.qipay.core.entity.DicConstance" %>
<%@ page import="com.qipay.core.utils.CommUtil" %>
<%@ page import="com.qipay.core.utils.SpringContextUtil" %>
<%@ page import="com.qipay.mapper.MenuMapper" %>
<%@ page import="java.util.List" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.Optional" %>
<%@ page import="java.util.ArrayList" %>
<%
    User user = (User) session.getAttribute("user");
    String icon = "1.jpg", userName = "未登录";
    int level = 0;
    String authCode = "";
    if (null != user) {
        icon = user.getSex() == 2 ? "2.jpg" : "1.jpg";
        userName = user.getName();
        level = user.getLevel();
        //if(level==500)level = 1000;
        authCode = user.getAuthCode() == null ? "" : user.getAuthCode();
    }

    MenuExample menuExample = new MenuExample();
    //取出有效菜单
    menuExample.or().andIsvalidEqualTo(DicConstance.SF_S);
    MenuMapper menuMapper = (MenuMapper) SpringContextUtil.getBean(MenuMapper.class);
    List<Menu> menus = menuMapper.selectByExample(menuExample);
    List<Menu> ms = new ArrayList<>();
    for (Menu m : menus) {
        try{
            List<Integer> auths = (List<Integer>) JSONObject.parse(Optional.ofNullable(m.getAuth()).orElse("[]"));
            if (auths.contains(level)) {
                ms.add(m);
            }
        }catch (Throwable e){
            System.out.println(m.getMenuId()+"這個id的菜单配置格式不对,不给展示"+e.getMessage());
        }
    }

    String menuDom = CommUtil.renderMenu(ms, request.getContextPath(), userName);
%>


<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">
<script type="text/javascript">
    document.getElementsByTagName("html")[0].style.fontSize = screen.availWidth / 200 + "px";
    console.log("${pageContext.request.contextPath}");
</script>
<!--#4D9064-->
<aside class="main-sidebar" style="background:#778899;">
    <!-- sidebar: style can be found in sidebar.less #00733e-->
    <section class="sidebar">
        <!-- Sidebar user panel -->
        <%--<div class="user-panel">--%>
            <%--<div class="pull-left image">--%>
                <%--<img src="<%= request.getAttribute("StaticHost") %>/img/<%= icon%>" class="img-circle" alt="User Image">--%>
            <%--</div>--%>
            <%--<div class="pull-left info">--%>
                <%--<p><%= userName%>--%>
                <%--</p>--%>
                <%--<a href="#"><% if (user != null) {%><i class="fa fa-circle text-success"></i> Online<%} else {%><i--%>
                        <%--class="fa fa-circle"></i> Offline<%}%></a>--%>
            <%--</div>--%>
        <%--</div>--%>
        <div id="mp3"></div>
        <script>
            $(function () {
                var level = "<%=level%>";
                if (level == 1000) {
                    //setInterval('findwidthdraw()',5000);
                }
            });
            var timer1

            function findwidthdraw() {
                $.ajax({
                    url: "${pageContext.request.contextPath}/ajax?n=findwidthdraw&id=10",
                    type: "GET",
                    async: true,//true异步 false同步
                    context: this,
                    timeout: 10000,
                    success: function (data) {
                        if (data != null && data > 0) {
                            //laba();
                            timer1 = setInterval("laba()", 5000);
                        }
                    },
                    error: function (d, s, b) {
                        //alert("失败:" + b);
                    }
                });
            }

            //喇叭提醒
            var num = 1;

            function laba() {
                if (num > 2) {
                    clearInterval(timer1);
                    num = 0;
                }
                if (navigator.userAgent.indexOf("Chrome") > -1) {
                    //alert("如果是Chrome");
                    $('#mp3').append('<audio src="<%= request.getAttribute("StaticHost") %>/audio/13.mp3" type="audio/mp3" autoplay=”autoplay” hidden="true"></audio>');
                } else if (navigator.userAgent.indexOf("Firefox") != -1) {
                    //alert("如果是Firefox");
                    $('#mp3').append('<embed src="<%= request.getAttribute("StaticHost") %>/audio/13.mp3" type="audio/mp3" hidden="true" loop="false" mastersound></embed>');
                } else if (navigator.appName.indexOf("Microsoft Internet Explorer") != -1 && document.all) {
                    //alert("如果是IE(6,7,8)");
                    //$('#mp3').append('<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95"><param name="AutoStart" value="1" /><param name="<%=request.getContextPath()%>/admin/MP3/25.mp3" value="" /></object>');
                    $('#mp3').append('<embed src="<%= request.getAttribute("StaticHost") %>/audio/13.mp3" type="audio/mp3" hidden="true" loop="false" mastersound></embed>');
                } else if (navigator.appName.indexOf("Opera") != -1) {
                    //alert("如果是Oprea");
                    $('#mp3').append('<embed src="<%= request.getAttribute("StaticHost") %>/audio/13.mp3" type="audio/mpeg"   loop="false"></embed>');
                } else {
                    //alert("360");
                    $('#mp3').append('<embed src="<%= request.getAttribute("StaticHost") %>/audio/13.mp3" type="audio/mpeg"   loop="false"></embed>');
                }
                num++;
            }
        </script>

        <!-- sidebar menu: : style can be found in sidebar.less -->
        <ul class="sidebar-menu" data-widget="tree" id="ulLeftMenu">
            <% if (user != null) {%>
            <%--<li class="header">MAIN NAVIGATION</li>--%>
            <%=menuDom%>
            <%--  <li class="header">信息</li>
              <li class="header" style="background: #138900;color: #fff;">信息</li>
              <li><a href="${pageContext.request.contextPath}/logout"><i class="fa fa-circle-o text-red"></i>
                  <span>注销</span></a></li>--%>
            <%}%>
        </ul>
    </section>
    <script>
        var qx = "<%=authCode%>";
        $(function () {
            if (qx != "") {
                var main_auth = $("#ulLeftMenu").find('.treeview');//导航栏的元素
                for (var i = 0; i < main_auth.length; i++) {
                    var ls_flag = true;
                    for (var j = 0; j < main_auth.eq(i).find("ul").children().length; j++) {
                        var value = i + "," + j;
                        if (!checkauth(value)) {
                            main_auth.eq(i).find("ul").children().eq(j).remove();
                        } else {
                            ls_flag = false;
                        }

                    }
                    if (ls_flag) {
                        main_auth.eq(i).remove();
                    }
                }
            }
        });

        //比对权限，加载页面的时候看看有没有选中
        // function checkauth(a) {
        //     if (qx != "") {
        //         var qxa = qx.split("-");
        //         for (var i = 1; i < qxa.length - 1; i++) {
        //             if (a == qxa[i]) {
        //                 return true;
        //             }
        //         }
        //     }
        // }
    </script>
    <!-- /.sidebar -->
</aside>